package com.csi.bumblebee.controller.backstage;

import com.csi.bumblebee.domain.manage.Account;
import com.csi.bumblebee.domain.manage.AccountRole;
import com.csi.bumblebee.domain.manage.Role;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.domain.util.StateCode;
import com.csi.bumblebee.service.ManageService;
import com.csi.bumblebee.util.IdUtil;
import com.csi.bumblebee.util.JwtUtil;
import net.minidev.json.JSONValue;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 孙伟俊 yeebs(登录)
 */
@RestController
@RequestMapping("/backstage/account")
public class BackstageAccountController {

    @Autowired
    private ManageService manageService;

    /**
     * 展示所有的管理员账号
     *
     * @return MyResult<List < Account>>
     */
    @GetMapping("/list")
    public MyResult<List<Account>> list() {

        MyResult<List<Account>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<Account> accountList = manageService.list();
        if (accountList != null) {
            myResult.setData(accountList);
            return myResult;
        } else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }
        return myResult;
    }

    /**
     * 增加管理员账号
     *
     * @param accountRole 传过来的account对象
     * @return MyResult<Integer>
     */
    @PostMapping("/account")
    public MyResult<Integer> add(@RequestBody AccountRole accountRole) {
        MyResult<Integer> myResult = new MyResult<>(StateCode.INSERT_FAILURE, "增加失败");

        //用户主键id
        String accountId = IdUtil.getString(Account.PRE_NAME);
        //用户角色主键id
        String accountRoleId = IdUtil.getString(AccountRole.PRE_NAME);

        Account account = accountRole.getAccount();
        account.setId(IdUtil.getString(Account.PRE_NAME));
        //先插入管理员
        int result1 = manageService.add(account);
        if (result1 > 0) {
            //设置用户角色主键
            accountRole.setId(accountRoleId);
            accountRole.setAccountId(accountId);
            int result2 = manageService.insertAccountRole(accountRole);
            if (result2 > 0) {
                myResult.setCode(StateCode.INSERT_SUCCESS);
                myResult.setMessage("插入成功");
            }else {
                return myResult;
            }
        }else {
            return myResult;
        }
        System.out.println("zoule");
        return null;
    }

    /**
     * 删除管理员账号
     *
     * @param id 主键Id
     * @return MyResult<Integer>
     */
    @DeleteMapping("/account/{id}")
    public MyResult<Integer> delete(@PathVariable String id) {
        MyResult<Integer> myResult = new MyResult<>(StateCode.DELETE_SUCCESS, "删除失败");
        int result = manageService.delete(id);

        if (result > 0) {
            return myResult;
        } else {
            myResult.setCode(StateCode.DELETE_FAILURE);
            myResult.setMessage("删除失败");
        }

        return myResult;
    }

    /**
     * 登录
     */
    @PostMapping("/entry")
    public MyResult<Account> login(@RequestBody Account account) {

        MyResult<Account> myResult = new MyResult<>(StateCode.PASSAPPLICATION_FAILURE, "登录失败");

        Account manager = manageService.login(account.getUsername(),account.getPassword());

        if (manager != null) {
            String token = JwtUtil.createToken(account.getUsername(), "管理员", 1);

            manager.setToken(token);

            manager.setPassword(null);

            myResult.setData(manager);
            myResult.setCode(StateCode.PASSAPPLICATION_SUCCESS);
            myResult.setMessage("登录成功");

            return myResult;
        }
        return myResult;
    }

    /**
     * 显示所有的角色
     * @return MyResult<List<Role>>
     */
    @GetMapping("/role")
    public MyResult<List<Role>> listAllRole(){
        System.out.println("12313123");
        MyResult<List<Role>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<Role> roleList = manageService.listAllRole();
        if (roleList != null){
            myResult.setData(roleList);
            return myResult;
        }else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }
        return myResult;
    }
}
